package client.gui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;

import javax.swing.*;

import messages.*;

import sharedspace.*;

import client.gui.GUI_AbstractMainMenu.MainMenuType;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * This JPanel is a panel for Teacher to see statistic of executive tests
 * @author Amir S.
 * @author Jacob C.
 * @author Maya G.
 * @author Michael V.
 *
 */
public class GUI_Teacher_View_Statistics<trList> extends GUI_AbstractMainMenu{
	public JButton btnBack = null;
	private JScrollPane dataScroller = null;
	private JTable dataJTable = null;
	private List<StatisticTestInformation> sList = null;
	private List<Teacher> tList = null;
	public List<ExecuteTest> exList = null;

	private JTable statisticJTable = null;

	public Client_GUI ms=null;
	
	public GUI_Teacher_View_Statistics(Client_GUI ms) {
		super(MainMenuType.MANAGER,ms);
		this.ms=ms;
		initiate();
		greetMsg.setText("View Statistic Reports");
		mainMenu.setText("Teacher");
		

	}
	
	private void initiate()
	{		
		background.add(getbtnBack(),null);
		background.add(btnBack);
		background.add( getStatisticTable());
		
	}
	
	private JButton getbtnBack()
	{
		if (btnBack == null)
		{
			btnBack = new JButton("Back");	
			btnBack.setBounds(53, 28, 107, 30);
			btnBack.setVisible(true);
		}
		
		return btnBack;
	}

	private JScrollPane getStatisticTable()
	{
		if (statisticJTable == null)
		{
			MessageExecutedExamListByAuthorID MsgGetl = new MessageExecutedExamListByAuthorID(ms.lg.getUser().getUserID());
			try {
				ms.client.sendToServer(MsgGetl);
				MessageExecutedExamListReply MsgRep =(MessageExecutedExamListReply) ms.client.GetMessage();
				exList = MsgRep.getExTestList();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			MessageStatisticInfoListByExecuteTestList MsgSibeti = new MessageStatisticInfoListByExecuteTestList(exList);
			try {
					ms.client.sendToServer(MsgSibeti);
					sList =(ArrayList<StatisticTestInformation>)ms.client.GetMessage();
				} 
			catch (IOException e) {
					e.printStackTrace();
				}
			
			String columnNames[] = {"ID","Avarage","Median","0-10","10-20","20-30", "30-40", "40-50", "50-60", "60-70", "70-80", "80-90", "90-100", "Student Number"};
			String tableModel[][] = new String[sList.size()][StatisticTestInformation.NUM_OF_COLUMNS];
			for (int i=0; i<sList.size(); i++){
				tableModel[i][0] = sList.get(i).getExecuteTestID();
				tableModel[i][1] = ""+sList.get(i).getTestAvarage();
				tableModel[i][2] = ""+sList.get(i).getTestMedian();
				for (int j = 3; j<13 ; j++)
					tableModel[i][j] = "" + sList.get(i).getGradeDistribution()[j-3];
				tableModel[i][13] = ""+sList.get(i).getCount();
			}
			statisticJTable = new JTable(tableModel, columnNames){
				   public boolean isCellEditable(int rowIndex, int colIndex) {
				   return false; //Disallow the editing of any cell
				   }
				   };		 
				   statisticJTable.setFont(new Font("Tahoma", Font.PLAIN, 14));
				   statisticJTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				   statisticJTable.getTableHeader().setReorderingAllowed(false);
				   statisticJTable.setEnabled(false);
			dataScroller= new JScrollPane(statisticJTable);
			dataScroller.setBounds(50, 150, 900, 500);
		}
			
		
		return dataScroller;
	}

}
